package com.tust.app.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.tust.app.domain.User;
import com.tust.app.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Controller
public class UserController {

    @Autowired
    private UserService userService;

    @RequestMapping("/upage")
    public String upage(HttpServletRequest request) {
        PageHelper.startPage(1, 3);
        List<User> users = userService.findAll();
        PageInfo<User> page = new PageInfo<>(users);
        request.setAttribute("pageInfo", page);
        return "user/home";
    }

    @RequestMapping("/upage/{pageNum}")
    public String upage2(@PathVariable("pageNum") Integer pageNum, HttpServletRequest request) {
        PageHelper.startPage(pageNum, 3);
        List<User> users = userService.findAll();
        PageInfo<User> page = new PageInfo<>(users);
        request.setAttribute("pageInfo", page);
        return "user/home";
    }

    @RequestMapping("/auser")
    public String listAdminUser(Integer pn, HttpServletRequest request) {
        if (pn == null) {
            pn = 1;
        }
        PageHelper.startPage(pn, 3);
        List<User> user = userService.findAll();
        PageInfo<User> page = new PageInfo<>(user);
        request.setAttribute("userList", page);
        return "admin/user/allUser";
    }

    @RequestMapping("/selectByUsername")
    public String selectByUsername(String username, HttpServletRequest request) {
        PageHelper.startPage(1, 8);
        List<User> searchList = userService.findByUsername(username);
        PageInfo<User> pageInfo = new PageInfo<>(searchList);
        request.setAttribute("userList", pageInfo);
        if (searchList != null) {
            return "admin/user/allUser";
        }
        return "error";
    }

    @RequestMapping("/getUser")
    public String getUser(Integer id, HttpServletRequest request) {
        User user = userService.findById(id);
        request.setAttribute("user", user);
        return "admin/user/edituser";
    }

    @RequestMapping(value = "/editUser", method = RequestMethod.GET)
    public String editUser(@RequestParam("id") Integer id, Model model) {
        User user = userService.findById(id);
        model.addAttribute("user", user);
        return "update";
    }

    @RequestMapping(value = "/updateUser", method = RequestMethod.POST)
    public String updateUser(@RequestParam("id") Integer id,
                             @RequestParam("username") String username,
                             @RequestParam("password") String password,
                             @RequestParam("email") String email) {
        // 创建用户对象并设置属性
        User user = new User();
        user.setId(id);
        user.setUsername(username);
        user.setPassword(password);
        user.setEmail(email);
        user.setState(1);
        user.setRoleId(1);

        boolean updated = userService.update(user);
        if (updated) {
            return "redirect:/login.jsp";  // 修改后的跳转路径
        }
        return "error";
    }



    @RequestMapping("/deleteUserById")
    public String deleteUserById(Integer id) {
        boolean deleted = userService.deleteById(id);
        if (deleted) {
            return "forward:/auser";
        }
        return "error";
    }

    @RequestMapping("/changeState")
    public String changeState(@RequestParam("username") String username) {
        List<User> users = userService.findByUsername(username);
        if (!users.isEmpty()) {
            User user = users.get(0); // 假设用户名唯一
            user.setState(user.getState() == 0 ? 1 : 0);
            userService.update(user);
        }
        return "redirect:/auser";
    }

    @RequestMapping("/changeRole")
    public String changeRole(@RequestParam("username") String username) {
        List<User> users = userService.findByUsername(username);
        if (!users.isEmpty()) {
            User user = users.get(0); // 假设用户名唯一
            user.setRoleId(user.getRoleId() == 1 ? 2 : 1);
            userService.update(user);
        }
        return "redirect:/auser";
    }

//
//    @RequestMapping("/selectUserById")
//    public String selectUserById(Integer id, HttpServletRequest request) {
//        User user = userService.findById(id);
//        request.setAttribute("user", user);
//        return "user/detail";
//    }
}